package com.example.demo.admin.service;

import com.example.demo.admin.bean.AdminBean;
import com.example.demo.admin.repository.AdminRepository;
import com.example.demo.security.bean.SecurityAdminRoleBean;
import com.example.demo.security.bean.SecurityResourceBean;
import com.example.demo.security.bean.SecurityRoleBean;
import com.example.demo.security.bean.SecurityRoleResourceBean;
import com.example.demo.security.service.SecurityAdminRoleService;
import com.example.demo.security.service.SecurityRoleService;
import com.example.demo.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class AdminService extends BaseService<AdminBean,AdminRepository>{


    @Autowired
    private SecurityRoleService roleService;

    @Autowired
    private SecurityAdminRoleService adminRoleService;


    public List<String> getRole(int id) {

        return this.getById(id).getSecurtiyAdminRoleBeans().parallelStream().map(b->String.valueOf(b.getSecurityRoleBean().getId())).collect(Collectors.toList());
    }


    public void save(AdminBean bean, String[] roleBeanSet) {

        Collection<Long> resCollection = Arrays.stream(roleBeanSet).mapToLong(Long::valueOf).boxed().collect(Collectors.toList());

        List<SecurityRoleBean> list = roleService.repository.findAll(resCollection);


        if (bean.getId()!=0){
            AdminBean temp = getById((int) bean.getId());
            bean = temp;
            Set<SecurityAdminRoleBean> oldRole = temp.getSecurtiyAdminRoleBeans();


            Set<SecurityAdminRoleBean> finalOldRole = oldRole;
            Set<SecurityAdminRoleBean> newRole =  adminRoleService.createAdminRoleBean(list);

            Set<SecurityAdminRoleBean> finalNewRole = newRole;

//            oldRoleResource.retainAll(newRoleResource);
            // 求交集

            oldRole = oldRole.stream().filter(o->
                    finalNewRole.stream().anyMatch(n->
                            o.getSecurityRoleBean().getId()==n.getSecurityRoleBean().getId()
                    )
            ).collect(Collectors.toSet());


//            newRoleResource.removeAll(oldRoleResource);
            Set<SecurityAdminRoleBean> removeSet = new HashSet<>();
            removeSet = finalOldRole.stream().filter(n->!finalNewRole.stream().anyMatch(o->
                            o.getSecurityRoleBean().getId()==n.getSecurityRoleBean().getId()
                    )
            ).collect(Collectors.toSet());

            // 求新的和旧的差集
            /**
             * 这里用removeAll移除 只会把roid置为null,并不能删除这条记录,感觉应该还是和练级存在关系
             */
//            bean.getRoleResourceBeanSet().removeAll(removeSet);
            adminRoleService.repository.deleteInBatch(removeSet);


            newRole = newRole.stream().filter(n->
                    !finalOldRole.stream().anyMatch(o->
                            o.getSecurityRoleBean().getId()==n.getSecurityRoleBean().getId()
                    )
            ).collect(Collectors.toSet());

            //xxx 这里好像会存在问题...
            bean.getSecurtiyAdminRoleBeans().addAll(newRole);


        }else{
            //新的管理员
            Set<SecurityAdminRoleBean> setroleresources = adminRoleService.createAdminRoleBean(list);
            bean.setSecurtiyAdminRoleBeans(setroleresources);
        }
        save(bean);
    }


   public AdminBean findByUserName(String username){
        return repository.findAdminBeanByUsername(username);
    }
}
